package com.ruoyi.web.controller.train;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.TrainingBase;
import com.ruoyi.system.service.ITrainingBaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 临床培训基地Controller
 *
 * @author dp
 * @date 2023-11-26
 */
@Api(tags = {"临床培训基地管理"})
@RestController
@RequestMapping("/train/base")
public class TrainingBaseController extends BaseController {
    @Autowired
    private ITrainingBaseService trainingBaseService;
    @Autowired
    TokenService tokenService;

    /**
     * 查询临床培训基地列表
     */
    @ApiOperation(value = "临床培训基地列表", notes = "查询临床培训基地列表详情", tags = {"临床培训基地管理"})
    //@PreAuthorize("@ss.hasPermi('train:base:list')")
    @GetMapping("/list")
    public TableDataInfo list(TrainingBase trainingBase) {
        startPage();
        List<TrainingBase> list = trainingBaseService.selectTrainingBaseList(trainingBase);
        return getDataTable(list);
    }

    /**
     * 导出临床培训基地列表
     */
    @ApiOperation(value = "临床培训基地列表导出", notes = "导出临床培训基地列表", tags = {"临床培训基地管理"})
    //@PreAuthorize("@ss.hasPermi('train:base:export')")
    @Log(title = "临床培训基地", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TrainingBase trainingBase) {
        List<TrainingBase> list = trainingBaseService.selectTrainingBaseList(trainingBase);
        ExcelUtil<TrainingBase> util = new ExcelUtil<TrainingBase>(TrainingBase.class);
        util.exportExcel(response, list, "临床培训基地数据");
    }

    /**
     * 获取临床培训基地详细信息
     */
    @ApiOperation(value = "临床培训基地详细信息", notes = "获取临床培训基地详细信息详情", tags = {"临床培训基地管理"})
    //@PreAuthorize("@ss.hasPermi('train:base:query')")
    @GetMapping(value = "/{baseId}")
    public AjaxResult getInfo(@PathVariable("baseId") Long baseId) {
        return success(trainingBaseService.selectTrainingBaseByBaseId(baseId));
    }

    /**
     * 新增临床培训基地
     */
    @ApiOperation(value = "新增临床培训基地", notes = "新增临床培训基地详情", tags = {"临床培训基地管理"})
    //@PreAuthorize("@ss.hasPermi('train:base:add')")
    @Log(title = "临床培训基地", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(HttpServletRequest request, @RequestBody TrainingBase trainingBase) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        trainingBaseService.completeData(trainingBase, userId, 1);
        return toAjax(trainingBaseService.insertTrainingBase(trainingBase));
    }

    /**
     * 修改临床培训基地
     */
    @ApiOperation(value = "修改临床培训基地", notes = "修改临床培训基地详情", tags = {"临床培训基地管理"})
    //@PreAuthorize("@ss.hasPermi('train:base:edit')")
    @Log(title = "临床培训基地", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(HttpServletRequest request, @RequestBody TrainingBase trainingBase) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        trainingBaseService.completeData(trainingBase, userId, 2);
        return toAjax(trainingBaseService.updateTrainingBase(trainingBase));
    }

    /**
     * 删除临床培训基地
     */
    @ApiOperation(value = "删除临床培训基地", notes = "删除临床培训基地详情", tags = {"临床培训基地管理"})
    //@PreAuthorize("@ss.hasPermi('train:base:remove')")
    @Log(title = "临床培训基地", businessType = BusinessType.DELETE)
    @DeleteMapping("/{baseIds}")
    public AjaxResult remove(@PathVariable Long[] baseIds) {
        return toAjax(trainingBaseService.deleteTrainingBaseByBaseIds(baseIds));
    }
}
